﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA.Entities;
using OpenGado.DATA;

namespace OpenGado.Win
{
    public partial class frmProjects : Form
    {

        public delegate void UpdateProjectListHandler();

        public UpdateProjectListHandler UpdateProjectList;

        Project p;

        public frmProjects()
        {
            InitializeComponent();
        }

        public frmProjects(int projectId)
        {
            InitializeComponent();

            //
            p = ProjectDataControl.Instance.GetById(projectId);

            // bind values 

            txtName.Text = p.Name;
            txtCoordinator.Text = p.Coordinator;
            txtCode.Text = p.Code;
            if (p.StartingDate != null)
            {
                dtpStartDate.Value = (DateTime)p.StartingDate;
            }
            if (p.EndDate != null)
            {
                dtpEndDate.Value = (DateTime)p.EndDate;
            }
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            SaveProject();

            this.Close();
        }

        private void SaveProject()
        {

            if (p != null)
            {
                p.Name = txtName.Text;
                p.Code = txtCode.Text;
                p.Coordinator = txtCoordinator.Text;
                p.StartingDate = dtpStartDate.Value;
                p.EndDate = dtpEndDate.Value;
                ProjectDataControl.Instance.Save(p);
            }
            else
            {
                Project project = new Project();
                project.Name = txtName.Text;
                project.Code = txtCode.Text;
                project.Coordinator = txtCoordinator.Text;
                project.StartingDate = dtpStartDate.Value;
                project.EndDate = dtpEndDate.Value;


                ProjectDataControl.Instance.Save(project);
            }
           
            // fire event
            UpdateProjectList();
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            if (dtpEndDate.Value == DateTime.Today)
            {
                dtpEndDate.Value = DateTime.Now.AddYears(5);
            }

        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }
    }
}
